/*
 * (C) Copyright 2012, by Tim Xie.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.superapk.diamond.util;
import java.util.Calendar;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

/**
 * App操作常用类
 * 
 * @author     Tim Xie [tim.xie2008@gmail.com]
 * @date       2013-2-21
 * 
 */

public class AppUtil {
	
	private static String deviceId = null;
	
	/**
	 * 检查引用是否存在
	 * @param context
	 * @param packageName 需要检查的类
	 * @return
	 */
	public static boolean existApp(Context context,String packageName){
		try {
        	// 获取packagemanager的实例  
            PackageManager packageManager = context.getPackageManager();  
			packageManager.getPackageInfo(packageName,0);
			return true;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}  
		return false;
	}
	
	/**
	 * 获取当前应用Icon
	 * @param context
	 * @return
	 */
	public static int getAppIconRes(Context context){
		// 获取packagemanager的实例  
		ApplicationInfo appInfo = context.getApplicationInfo();
		return appInfo.icon;  
	}
	
	/**
	 * 获取应用的版本号
	 * @return
	 */
	public static String getAppVersionCode(Context context){
		String versionCode = null;
        try {
        	// 获取packagemanager的实例  
            PackageManager packageManager = context.getPackageManager();  
            // getPackageName()是你当前类的包名，0代表是获取版本信息  
			PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(),0);
			versionCode = Integer.toString(packInfo.versionCode);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}  
        return versionCode;
	}
	
	/**
	 * 获取应用版本名称
	 * @param context
	 * @return
	 */
	public static String getAppVersionName(Context context){
		String versionName = null;
        try {
        	// 获取packagemanager的实例  
            PackageManager packageManager = context.getPackageManager();  
            // getPackageName()是你当前类的包名，0代表是获取版本信息  
			PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(),0);
			versionName = packInfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}  
        return versionName;
	}
	
	/**
	 * 获取设置的ID
	 * @param context
	 * @return
	 */
	public static String getDeviceId(Context context){
		if(deviceId != null){
			return deviceId;
		}
		TelephonyManager localTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		if (localTelephonyManager == null) {
			LogUtil.e("Can't find IMEI.");
		} else{
			try {
				deviceId = localTelephonyManager.getDeviceId();
			} catch (SecurityException se) {
				LogUtil.e(se.toString());
			}
			if (deviceId == null) {
				try {
					WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
					WifiInfo info = wifi.getConnectionInfo();
					deviceId = info.getMacAddress();
				} catch (SecurityException se) {
					LogUtil.e(se.toString());
				}
			}
			if(deviceId == null || deviceId.equals("")){
				deviceId = "000000000000000";
			}
		}
		return deviceId;
	}
	
	/**
	 * 获取当前时区
	 * @param context
	 * @return
	 */
	public static String getTimeZone(Context context) {
		Configuration configuration = context.getResources().getConfiguration();
		int timeZome = 8;
		if (configuration != null){
			// 获取时区
			Calendar calendar = Calendar.getInstance(configuration.locale);
			if (calendar != null) {
				timeZome = calendar.getTimeZone().getRawOffset() / (60 * 60 * 1000);
			} else
				timeZome = 8;
		}
		return Integer.toString(timeZome);
	}
	
}
